package com.supermap.dataservice.webservice;

import java.io.*;
import java.util.Vector; 
import javax.xml.parsers.*; 
import org.w3c.dom.*;   //大佬提供的解析代码 泪崩
public class XmlRead { 
static Document document; 
private boolean validating; 
public XmlRead() { 
} 
public Vector toRead(String filename) {  //filename textcontent
Vector title=new Vector(); 
Vector content=new Vector(); 
String myStr=new String(); 
try { 
	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
	factory.setValidating(validating); 
	DocumentBuilder builder = factory.newDocumentBuilder(); 
//	document = builder.parse(new File(filename));
	
	byte[] bytes=new String(filename).getBytes();     //String.getBytes()返回一 个字节数组。
	ByteArrayInputStream bin=new ByteArrayInputStream(bytes);
//	DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//	DocumentBuilder builder=factory.newDocumentBuilder();
	document=builder.parse(bin);
		
	document.getDocumentElement().normalize(); 
	Node node = document.getFirstChild(); 
	NodeList list = node.getChildNodes(); 
	for (int i = 0; i < list.getLength(); i++) { 
		Node nodeitm = list.item(i); 
		System.out.println("nodeitm.getNodeName():"+nodeitm.getNodeName());
		if (nodeitm.getNodeName().equals("CityCode")) { 
//		myStr=nodeitm.getFirstChild().getNodeValue(); 
		myStr=nodeitm.getAttributes().item(0).getNodeValue();
		title.addElement(myStr);//getFirstChild() 
		} 
		if (nodeitm.getNodeName().equals("forecastTime")) { 
		myStr=nodeitm.getFirstChild().getNodeValue(); 
		content.addElement(myStr); 
		} 
		
		NodeList list2 = nodeitm.getChildNodes();
		for (int j = 0; j < list2.getLength(); j++) {
			Node nodeitm2 = list2.item(j); 
			System.out.println("nodeitm.getNodeName():"+nodeitm2.getNodeName());
			if (nodeitm2.getNodeName().equals("forecastTime")) { 
//				myStr=nodeitm.getFirstChild().getNodeValue(); 
				myStr=nodeitm2.getAttributes().item(0).getNodeValue();
				content.addElement(myStr);//getFirstChild() 
				}
		}
	}
	
} catch (Exception exp) { 
exp.printStackTrace(); 
return null; 
} 
Vector all=new Vector(); 
all.add(title); 
all.add(content); 
return all; 
}

public static void main(String[] args) { 
Vector A; 
XmlRead my = new XmlRead(); 
String teststr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT><CityCode CityCode=\"57516\"><forecastTime forecastTime=\"20200421\"><weatherDescription>小雨</weatherDescription><DryingIndeX>晾晒指数预报:一级，有降雨，不适宜晾晒。</DryingIndeX><CityFireIndeX>城市火险指数预报：一级，物体难于着火，但要注意人为火灾，防火人员必须坚守岗位，以防不测。</CityFireIndeX><wishingCarIndeX>洗车指数预报:三级，不宜洗车，未来有降雨，雨水和路上的泥水会再次弄脏您的爱车。</wishingCarIndeX><WarehouseFireIndeX>仓库火险指数预报：一级，不易燃烧，请消险防患，防患于未然。</WarehouseFireIndeX><drinkIndex>饮料指数预报:三级，适宜喝乳酸或低糖系列饮料。</drinkIndex><dressIndex>穿衣指数预报:三级，适宜穿着单层棉麻面料的短套装、T恤衫、薄牛仔衫裤、休闲服、职业套装。</dressIndex><feverIndex>感冒指数预报:四级，可能感冒，体质较弱的朋友请注意预防，可适度运动锻炼。</feverIndex><tourIndex>旅游指数预报:六级，极不适宜旅游，若要出游请注意携带雨具。</tourIndex><morningExecIndex>晨练指数预报:一级，夜间有降雨，地面积水，不适宜晨练。</morningExecIndex><cozyIndex>舒适度指数预报:五级，感觉稍凉，比较舒适，早晚间比较冷，请注意适时添衣。</cozyIndex><humidityIndex>霉变指数预报:四级，霉变指数偏大，空气潮湿，大部分物品易发生霉变，请妥善保存，注意防霉。</humidityIndex><driveIndex>行驶指数预报:四级，有降雨，部分路面较为湿滑，请注意减速行驶。</driveIndex><beerIndex>夜啤酒指数预报:三级，较适宜。</beerIndex><MakeupIndex>花妆指数预报:三级，较易花妆。</MakeupIndex><LookIndex>打望指数预报:二级，较不适宜。雨天打望备好透明雨伞，以免遮挡视线。</LookIndex></forecastTime></CityCode><CityCode CityCode=\"57517\"/></ROOT>";
A = my.toRead(teststr); 

for (int i = 0; i < A.size(); i++) { 
System.out.println(A.elementAt(i)); 
} 
} 
} 
